#pragma once
#include"RBTree.h"
namespace yh {
	template<class K, class V>
	class map {
		struct MapKeyOfT {
			const K& operator()(const pair<const K, V>& kv) {
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::iterator iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::const_iterator const_iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::reverse_iterator reverse_iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::const_reverse_iterator const_reverse_iterator;
		pair<iterator, bool> insert(const pair<const K, V>& kv) {
			return _t.insert(kv);
		}
		V& operator[](const K& key) {
			pair<iterator, bool> m = insert(pair<const K, V>(key, V()));
			return m.first->second;
		}
		const_iterator cbegin()const{
			return _t.cbegin();
		}
		const_iterator cend()const{
			return _t.cend();
		}

		iterator begin() {
			return _t.begin();
		}
		iterator end() {
			return _t.end();
		}

		reverse_iterator rbegin() {
			return _t.rbegin();
		}
		reverse_iterator rend() {
			return _t.rend();
		}

		const_reverse_iterator crbegin()const {
			return _t.crbegin();
		}
		const_reverse_iterator crend()const {
			return _t.crend();
		}
	private:
		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
	};
	void test_map1() {
		int a[] = { 1,2,3,4,5,6,7,8,9,10 };
		map<int,int> m1;
		for (int i = 0; i < 10; i++) {
			m1.insert(pair<int,int>(a[i],a[i]));
		}
		map<int,int>::const_reverse_iterator it = m1.crbegin();
		while (it != m1.crend()) {
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}
}
